using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class TrailManager : MonoBehaviour
{
    private TrailRenderer tr;
    private EdgeCollider2D edgeCollider2D;
    private List<Vector2> edgeColliderPosList = new List<Vector2>();

    void Start()
    {
        // // 控制子物体上的TrailRenderer
        // tr = transform.GetChild(0).GetComponent<TrailRenderer>();
        for (int i = 0; i < transform.childCount; i++)
        {
            string TsName = transform.GetChild(i).GetComponent<Transform>().name;
            if (TsName.Contains("Edge"))
            {
                edgeCollider2D = transform.GetChild(i).GetComponent<EdgeCollider2D>(); // 层级不对不能发生碰撞？
            }
            if (TsName.Contains("Trail"))
            {
                tr = transform.GetChild(i).GetComponent<TrailRenderer>();
            }
        }
        if (GetComponent<EdgeCollider2D>() != null)
        {
            edgeCollider2D = GetComponent<EdgeCollider2D>();
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            tr.enabled = false;
            edgeCollider2D.enabled = false;
        }
        // // 射线检测来隔断绳子
        // if (Input.GetMouseButton(0))
        // {
        //     // 鼠标脱离屏幕？
        //     Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        //     tr.transform.position = pos;
        //     tr.enabled = true;
        //     // Physics2D？
        //     RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
        //     if (hit.collider != null)
        //     {
        //         if (hit.collider.gameObject.CompareTag("Joint"))
        //         {
        //             Transform colliderTs = hit.collider.transform;
        //             int cutIndex = colliderTs.GetSiblingIndex();
        //             LineManager lm = colliderTs.parent.GetComponent<LineManager>();
        //             lm.cut = true;
        //             lm.cutIndex = cutIndex;
        //             string beCutLineName = colliderTs.parent.GetComponent<Transform>().name;
        //             Debug.Log("beCutLineName");
        //             Debug.Log(beCutLineName);
        //             lm.beCutLineName = beCutLineName;
        //         }
        //     }
        // }
        if (Input.GetMouseButton(0))
        {
            Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            tr.transform.position = pos;
            tr.enabled = true;

            edgeColliderPosList.Add(pos);
            // Debug.Log("pos");
            // Debug.Log(pos);
            edgeCollider2D.SetPoints(edgeColliderPosList);
            // Debug.Log("edgeCollider2D");
            // Debug.Log(edgeCollider2D);
            edgeCollider2D.enabled = true;
        }
        if (Input.GetMouseButtonUp(0))
        {
            edgeColliderPosList.Clear();
            edgeCollider2D.enabled = false;
        }
    }

    private void OnTriggerEnter2D(Collider2D otherGameObject)
    {
        Debug.Log("OnTriggerEnter2D JointJoint");
        if (otherGameObject.CompareTag("Joint"))
        {
            Transform colliderTs = otherGameObject.transform;
            int cutIndex = colliderTs.GetSiblingIndex();
            LineManager lm = colliderTs.parent.GetComponent<LineManager>();
            lm.cut = true;
            lm.cutIndex = cutIndex; // 被切的index
            string beCutLineName = colliderTs.parent.GetComponent<Transform>().name;
            // Debug.Log("beCutLineName");
            // Debug.Log(beCutLineName);
            lm.beCutLineName = beCutLineName;
        }
    }
}
